package com.hgyc.mom.qms.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.base.entity.Material;
import com.hgyc.mom.base.service.MaterialService;
import com.hgyc.mom.base.vo.MaterialParam;
import com.hgyc.mom.common.util.StringUtils;
import com.hgyc.mom.qms.vo.InspectionTerminalVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsInspectionTerminal;
import com.hgyc.mom.qms.vo.QmsInspectionTerminalParam;
import com.hgyc.mom.qms.service.QmsInspectionTerminalService;
import com.hgyc.mom.qms.vo.QmsInspectionTerminalVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 质量检测终端 Controller
 *
 * @author fwj
 * @since 2025-07-02
 */
@Tag(name = "质量检测终端 控制器", description = "质量检测终端 控制器")
@RestController
@RequestMapping("/qms/terminal")
public class QmsInspectionTerminalController {
    @Autowired
    private QmsInspectionTerminalService qmsInspectionTerminalService;

    @Autowired
    private MaterialService materialService;

    @Operation(summary = "查询质量检测终端 列表", description = "查询质量检测终端 列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsInspectionTerminal>> pageList(@Parameter(description = "查询参数") @RequestBody QmsInspectionTerminalParam qmsInspectionTerminalParam) {
        Page<QmsInspectionTerminal> page = new Page<>(qmsInspectionTerminalParam.getPage(), qmsInspectionTerminalParam.getPageSize());
        QmsInspectionTerminal qmsInspectionTerminal = new QmsInspectionTerminal();
        BeanUtils.copyProperties(qmsInspectionTerminalParam, qmsInspectionTerminal);
        QueryWrapper<QmsInspectionTerminal> wrapper = QueryWrapperUtil.build(qmsInspectionTerminal);
        IPage<QmsInspectionTerminal> list = qmsInspectionTerminalService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取质量检测终端 详细信息", description = "获取质量检测终端 详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "质量检测终端 ID") @PathVariable("id") Long id) {
        return Result.success(qmsInspectionTerminalService.getById(id));
    }

    @Operation(summary = "新增质量检测终端 ", description = "新增质量检测终端 ")
    @PostMapping
    public Result<?> save(@Parameter(description = "质量检测终端 ") @RequestBody QmsInspectionTerminalVO qmsInspectionTerminalVO) {
        qmsInspectionTerminalService.saveTerminal(qmsInspectionTerminalVO);
        return Result.success();
    }

    @Operation(summary = "修改质量检测终端 ", description = "修改质量检测终端 ")
    @PutMapping
    public Result<?> update(@Parameter(description = "质量检测终端 ") @RequestBody QmsInspectionTerminalVO qmsInspectionTerminalVO) {
        qmsInspectionTerminalService.updateTerminal(qmsInspectionTerminalVO);
        return Result.success();
    }

    @Operation(summary = "删除质量检测终端 ", description = "删除质量检测终端 ")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(qmsInspectionTerminalService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "获取质量检测终端 详细信息", description = "获取质量检测终端 详细信息")
    @GetMapping(value = "/info/{ip}")
    public Result<InspectionTerminalVO> getInfoByIp(@Parameter(description = "质量检测终端 ID") @PathVariable("ip") String ip) {
        return Result.success(qmsInspectionTerminalService.getInfoByIp(ip));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsInspectionTerminal> util = new ExcelUtil<>(QmsInspectionTerminal.class);
        util.importTemplateExcel(response, "质量检测终端 ");
    }

    @Operation(summary = "导出质量检测终端 列表", description = "导出质量检测终端 列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsInspectionTerminalParam qmsInspectionTerminalParam) {
        QmsInspectionTerminal qmsInspectionTerminal = new QmsInspectionTerminal();
        BeanUtils.copyProperties(qmsInspectionTerminalParam, qmsInspectionTerminal);
        QueryWrapper<QmsInspectionTerminal> wrapper = QueryWrapperUtil.build(qmsInspectionTerminal);
        List<QmsInspectionTerminal> list = qmsInspectionTerminalService.list(wrapper);
        ExcelUtil<QmsInspectionTerminal> util = new ExcelUtil<>(QmsInspectionTerminal.class);
        util.exportExcel(response, list, "质量检测终端 数据");
    }

    @Operation(summary = "导入质量检测终端 列表", description = "导入质量检测终端 列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsInspectionTerminal> util = new ExcelUtil<>(QmsInspectionTerminal.class);
        List<QmsInspectionTerminal> qmsInspectionTerminalList = util.importExcel(file.getInputStream());
        String message = qmsInspectionTerminalService.importData(qmsInspectionTerminalList, updateSupport);
        return Result.success(message);
    }

    @PostMapping("/material/pageList")
    public Result<IPage<Material>> list(MaterialParam materialParam){
        Page<Material> page = new Page<>(materialParam.getPage(), materialParam.getPageSize());

        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(materialParam.getMaterialCode())) {
            wrapper.like(Material::getMaterialName, materialParam.getMaterialCode())
                    .or().like(Material::getMaterialCode, materialParam.getMaterialCode());
        }

        IPage<Material> list = materialService.page(page, wrapper);
        return Result.success(list);
    }
}
